package com.example.xianji.activity;


import java.util.ArrayList;

import com.example.xianji.R;
import com.example.xianji.adapter.ViewPagerAdapter;
import com.example.xianji.utils.BitmapDecodeUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 和js交互的图片显示页面
 *
 * @author me
 */
public class ShowWebImageActivity extends Activity {
    private ViewPager view_pager;
    private String image_path;
    private ArrayList<ImageView> list = new ArrayList<ImageView>();
    private RelativeLayout big_imge_back;
    private ImageView image_one_yinying, image_two_yinying;
    private String path;
    private TextView jsshowPage;
    /**
     * 当前的index
     */
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_show_web_image);

        view_pager = (ViewPager) findViewById(R.id.view_pager);
        jsshowPage = (TextView) findViewById(R.id.jsshowPage);
        image_path = getIntent().getStringExtra("image");
        currentIndex = getIntent().getIntExtra("index", 0) + 1;

        jsshowPage.setText(new StringBuffer(currentIndex + "").append(" / ").append(getIntent().getStringArrayExtra("picture").length + ""));
        for (int i = 0; i < getIntent().getStringArrayExtra("picture").length; i++) {
            path = getIntent().getStringArrayExtra("picture")[i];
            Bitmap bitmap = null;
            ImageView image = new ImageView(ShowWebImageActivity.this);
            if (path.contains("http")) {
                ImageLoader.getInstance().displayImage(path, image);
            } else {

                bitmap = BitmapDecodeUtil.decodeBitmap(this, path);
                image.setImageBitmap(bitmap);
                bitmap = null;
            }
            image.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ShowWebImageActivity.this.finish();
                }
            });
            list.add(image);
        }
        view_pager.setAdapter(new ViewPagerAdapter(list, this));
        view_pager.setCurrentItem(getIntent().getIntExtra("index", 0));

        view_pager.addOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                int totalinext = arg0 + 1;
                jsshowPage.setText(new StringBuffer(totalinext + "").append(" / ").append(getIntent().getStringArrayExtra("picture").length + ""));
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

    }


}
